/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.rpc.config;

import io.iec.edp.caf.commons.event.config.EventListenerData;
import io.iec.edp.caf.commons.event.config.EventListenerSettings;
import io.iec.edp.caf.commons.event.config.EventManagerData;
import io.iec.edp.caf.rest.server.RESTEndpointUtil;
import io.iec.edp.caf.rpc.api.event.RpcClientEventBroker;
import io.iec.edp.caf.rpc.api.event.RpcServerEventBroker;
import io.iec.edp.caf.rpc.api.event.RpcServerEventManager;
import io.iec.edp.caf.rpc.api.service.InternalServiceManageService;
import io.iec.edp.caf.rpc.api.service.RpcServer;
import io.iec.edp.caf.rpc.interceptor.AuthorizingInterceptor;
import io.iec.edp.caf.rpc.remote.http.api.RpcServiceApi;
import io.iec.edp.caf.rpc.remote.http.discover.CenterAddressDiscover;
import io.iec.edp.caf.rpc.remote.http.discover.RpcAddressDiscover;
import io.iec.edp.caf.rpc.remote.http.service.RpcRemoteInvokerImpl;
import io.iec.edp.caf.rpc.remote.http.service.RpcServiceApiImpl;
import io.iec.edp.caf.rpc.server.RpcServerImpl;
import io.iec.edp.caf.rpc.server.invoker.RpcRemoteInvoker;
import org.apache.cxf.Bus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration(proxyBeanMethods = false)
//@EnableJpaRepositories("io.iec.edp.caf.rpc.registry.storage.database.domain.repository")//配置repo的扫描路径
//@EntityScan("io.iec.edp.caf.rpc.registry.storage.database.domain.entity")
public class RpcServerAutoConfiguration {

    @Value("${msu.enable:false}")
    private Boolean msuEnable;

//    @Bean
//    public RESTEndpoint rpcServerRest(RpcServiceApi rest) {
//        return new RESTEndpoint("/runtime/communication/v1.0/rpc", rest);
//    }

//    @Bean
////    @ConditionalOnBean(RpcServerEventBroker.class)
//    public RpcServiceApi createRpcServerApi(Bus bus, RpcServer rpcServer, ICAFContextService cafContext, RpcServerEventBroker serverEventBroker) {
//        RpcServiceApi service = new RpcServiceApiImpl(rpcServer, serverEventBroker, cafContext);
//        //发布RESTService
//        RESTEndpointUtil.createJAXRSServerWithInterceptor("/runtime/communication/v1.0/rpc", bus,
//                Arrays.asList(service), Arrays.asList(new AuthorizingInterceptor()));
//        return service;
//    }
    @Bean
    //    @ConditionalOnBean(RpcServerEventBroker.class)
    public RpcServiceApi createRpcServerApi(Bus bus, RpcServer rpcServer, RpcServerEventBroker serverEventBroker) {
        RpcServiceApi service = new RpcServiceApiImpl(rpcServer, serverEventBroker);
        //发布RESTService
        RESTEndpointUtil.createJAXRSServerWithInterceptor("/runtime/communication/v1.0/rpc", bus,
                Arrays.asList(service), Arrays.asList(new AuthorizingInterceptor()));
        return service;
    }

    @Bean
    public RpcServer createRpcServer(InternalServiceManageService management) {
        return new RpcServerImpl(management);
    }

    @Bean
    public RpcServerEventManager createRpcServerEventManager() {
        return new RpcServerEventManager();
    }

    @Bean
//    @ConditionalOnBean(EventListenerSettings.class)
    public RpcServerEventBroker createRpcServerEventBroker(RpcServerEventManager manager, EventListenerSettings settings) {
        return new RpcServerEventBroker(manager, settings);
    }

//    该bean这次改造后没再使用了，该bean是内部用的，先暂时不发布。
//    @Bean
//    public RpcAddressDiscover createRpcAddressDiscover(MsuProperties properties, ServiceDiscovery serviceDiscovery) {
//        return new CenterAddressDiscover(properties, serviceDiscovery);
//    }


    @Bean
    public RpcRemoteInvoker createRpcRemoteInvoker(RpcClientEventBroker clientEventBroker) {
        return new RpcRemoteInvokerImpl(clientEventBroker);
    }

    @Bean
    public EventManagerData createRpcServerManagerData() {
        //声明EventManager
        EventManagerData managerData = new EventManagerData();
        //为EventManager设置名称
        managerData.setName("RpcServerEventManager");
        //声明EventListener
        EventListenerData listenerData = new EventListenerData();
        //为EventListener设置名称
        listenerData.setName("RpcServerLoggingListener");
        //设置步骤1中实现类的类名
        listenerData.setImplClassName("io.iec.edp.caf.rpc.server.listener.RpcServerLoggingListener");
        List<EventListenerData> listeners = new ArrayList<>();
        listeners.add(listenerData);
        //将Listener注册到EventManager
        managerData.setListeners(listeners);
        return managerData;
    }

}
